package com.roy.galaxy.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-31 09:11
 */
public class 罗马数转数组 {

    /**
     * 空间复杂度o(n) 时间复杂度o(n)
     * @param s
     * @return
     */
    public int romanToInt(String s) {
        // 算法一：判断后面数值是否大于前面
        Map<String, Integer> romaNumber = new HashMap<>();
        romaNumber.put("I", 1);
        romaNumber.put("IV",4);
        romaNumber.put("V", 5);
        romaNumber.put("IX",9);
        romaNumber.put("X", 10);
        romaNumber.put("XL",40);
        romaNumber.put("L", 50);
        romaNumber.put("LC", 50);
        romaNumber.put("C", 100);
        romaNumber.put("CD", 400);
        romaNumber.put("D", 500);
        romaNumber.put("DM", 900);
        romaNumber.put("M", 1000);

        Integer firstValue = 0;
        Integer nextValue = 0;
        int sum = 0;

        for (int i = 0; i < s.length(); i++){

            String temp = "";
            if(i != s.length()-1) {
                temp = s.charAt(i) + "" + s.charAt(i + 1);
            }
            else {
                temp = s.charAt(i)+"";
            }
            firstValue = romaNumber.get(temp);
            if( firstValue != null){
                sum += firstValue;
                i ++;
            } else {
                nextValue = romaNumber.get(s.charAt(i)+"");
                sum += nextValue;

            }
        }
        return sum;
    }

    public static void main(String[] args) {
       System.out.print( new 罗马数转数组().romanToInt("III"));
    }

}
